require 'xmlrpc/client'

unless ARGV.length == 5
  STDERR.puts "Usage: #{ARGV[0]} <host address> <xml-rpc path> <port> <nick>"
  exit 1
end

username = ARGV[4]

server = XMLRPC::Client.new(ARGV[1], ARGV[2], ARGV[3])

Shoes.app :width => 420, :height => 290 do
	@id = 1
	@chat_log_str = ""
	flow :margin => 10 do
		@chat_log = edit_box "", :width => "100%", :height => 240
		@message = edit_line "", :width => 300
		button "Send", :width => -300 do
			server.call("Chat.SendMessage", username, @message.text())
			@message.text = ""
		end
		animate(1) do
			while true do
				row = server.call("Chat.GetMessage", @id).to_s
				puts row
				if row.length > 0
					row = row.split(";")
					@chat_log_str = @chat_log_str + row[1] + ": " + row[2] + "\n"
					@chat_log.text = @chat_log_str
					@id = @id + 1
				else
					break
				end
			end

		end
	end
end
